import React from 'react';
import {TreeComp, EditableTree} from 'jh-lib';
import {getRandomKey} from "jh-utils";
import {Card} from "antd";

const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
    const preKey = _preKey || '0';
    const tns = _tns || gData;

    const children = [];
    for (let i = 0; i < x; i++) {
        const code = `${preKey}-${i}`;
        const tO = {title: code, key: code, name: code, code: code, isn: code, parentIsn: _preKey};
        tO.CAN_ADD = false;
        tO.CAN_EDIT = preKey === '0';
        tns.push(tO);
        if (i < y) {
            children.push(code);
        }
    }
    if (_level < 0) {
        return tns;
    }
    const level = _level - 1;
    children.forEach((code, index) => {
        tns[index].children = [];
        return generateData(level, code, tns[index].children);
    });
};
generateData(z);

const dataList = [];
const generateList = (data) => {
    for (let i = 0; i < data.length; i++) {
        const node = data[i];
        const {code} = node;
        dataList.push({key: code, title: code});
        if (node.children) {
            generateList(node.children);
        }
    }
};
generateList(gData);

const getParentKey = (code, tree) => {
    let parentKey;
    for (let i = 0; i < tree.length; i++) {
        const node = tree[i];
        if (node.children) {
            if (node.children.some(item => item.code === code)) {
                parentKey = node.code;
            } else if (getParentKey(code, node.children)) {
                parentKey = getParentKey(code, node.children);
            }
        }
    }
    return parentKey;
};

const fetchTree = ({search, node}) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve([
                {key: getRandomKey() + 'Child1', code: 'Child1', name: 'Child Node1', title: 'Child Node1'},
                {key: getRandomKey() + 'Child2', code: 'Child2', name: 'Child Node2', title: 'Child Node2'},
                {key: getRandomKey() + 'Child3', code: 'Child3', name: 'Child Node2', title: 'Child Node3'},
            ]);
        }, 1000);
    });
};

export default class Test extends React.Component {

    render() {
        // gData[0].isPopover=true;
        // gData[0].popProps={
        //     title:'123',
        //     content:<div>56566</div>
        // };
        return (
            <>
                <Card title={'TreeComp'} style={{marginBottom: 16}}>
                    <TreeComp
                        style={{height: 200}}
                        compData={gData}
                        // fetchTree={fetchTree}
                        isSearch={true}
                        onSelect={(values) => {
                            console.log('选中回调', values)
                        }}
                    />
                </Card>
                <Card title={'EditableTree'}>
                    <EditableTree
                        style={{height: 500, width: 250}}
                        compData={gData}
                        canAddRoot={false}
                        onSelect={(values) => {
                            console.log('onSelect', values);
                        }}
                        afterSelect={(values) => {
                            console.log('afterSelect', values)
                        }}
                        afterDeleteNode={() => {
                            console.log('afterDeleteNode')
                        }}
                        onAddNode={(newNode) => {
                            return {...newNode, name: '自定义名称'};
                        }}
                        afterAddNode={(newNode) => {
                            // alert('afterAddNode');
                        }}
                        onSaveNode={(node) => {
                            return {...node, name: '自定义Save名称'};
                        }}
                        onExpand={(expandedKeys, moreProps) => {
                            console.log('onExpand', expandedKeys, moreProps)
                        }}
                    />
                </Card>
            </>)
    }
}
